package com.fashion.web.actions.management;

import com.fashion.model.Video;
import com.fashion.service.VideoService;
import com.fashion.utils.StringUtils;
import com.fashion.utils.VideoResult;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.*;

/**
 * User: Administrator
 * Date: 12-4-12
 */
@Scope("prototype")
@Controller
@ParentPackage("json-default")
@Namespace("/management/video")
public class VideoAction extends ActionSupport {
    private VideoService videoService;
    private Map<String, Object> dataGridView;
    private int page;
    private int rows;
    private String videoName;
    private VideoResult videoResult;
    private Video videoForm;
    private String jsonResult;
    private String id;
    private Video video;

    @Resource(name = "videoService")
    public void setVideoService(VideoService videoService) {
        this.videoService = videoService;
    }

    @Action(value = "videoIndex", results = {@Result(name = SUCCESS, location = "video_index.jsp")})
    public String videoIndex() throws Exception {
        return SUCCESS;
    }

    @Action(value = "previewVideo", results = {@Result(name = SUCCESS, location = "video_preview.jsp")})
    public String previewVideo() throws Exception {
        video = videoService.findVideoById(id);

        return SUCCESS;
    }

    @Action(value = "fetchVideos", results = {@Result(name = SUCCESS, type = "json", params = {"root", "dataGridView"})})
    public String fetchVideos() throws Exception {
        List<Video> videoList = videoService.searchVideos(videoName, page, rows);
        int newsCount = videoService.countVideos(videoName, page, rows);
        dataGridView = new HashMap<String, Object>();
        dataGridView.put("rows", videoList);
        dataGridView.put("total", newsCount);

        return SUCCESS;
    }

    @Action(value = "videoForm", results = {@Result(name = SUCCESS, location = "../../form_result.jsp")})
    public String videoForm() throws Exception {
        videoResult = new VideoResult();

        String id = videoForm.getId();
        if (null == id || id.isEmpty()) {
            videoForm.setId(StringUtils.createGuid());
            videoForm.setCreateDateTime(new Date());
            videoResult.setEdit(false);
        } else {
            videoResult.setEdit(true);
        }

        videoService.saveOrUpdate(videoForm);
        videoResult.setSuccessful(true);

        JSONObject jsonObject = JSONObject.fromObject(videoResult);
        jsonResult = jsonObject.toString();

        return SUCCESS;
    }

    @Action(value = "deleteVideo", results = {@Result(name = SUCCESS, type = "json", params = {"root", "videoResult"})})
    public String deleteVideo() throws Exception {
        videoResult = new VideoResult();
        try {
            videoService.deleteVideoById(id);
            videoResult.setSuccessful(true);
        } catch (Exception e) {
            videoResult.setSuccessful(false);
            videoResult.setErrorMessage(e.getMessage());
        }

        return SUCCESS;
    }

    public Map<String, Object> getDataGridView() {
        return dataGridView;
    }

    public void setDataGridView(Map<String, Object> dataGridView) {
        this.dataGridView = dataGridView;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getVideoName() {
        return videoName;
    }

    public void setVideoName(String videoName) {
        this.videoName = videoName;
    }

    public VideoResult getVideoResult() {
        return videoResult;
    }

    public String getJsonResult() {
        return jsonResult;
    }

    public void setVideoForm(Video videoForm) {
        this.videoForm = videoForm;
    }

    public Video getVideoForm() {
        return videoForm;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Video getVideo() {
        return video;
    }

    public void setVideo(Video video) {
        this.video = video;
    }
}
